package com.sakiko.audiobook.user.api;

import com.sakiko.audiobook.common.login.SakiLogin;
import com.sakiko.audiobook.common.result.Result;
import com.sakiko.audiobook.common.util.AuthContextHolder;
import com.sakiko.audiobook.user.service.UserInfoService;
import com.sakiko.audiobook.vo.user.UserInfoVo;
import com.sakiko.audiobook.vo.user.UserPaidRecordVo;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

@Tag(name = "用户管理接口")
@RestController
@RequestMapping("api/user/userInfo")
@SuppressWarnings({"all"})
public class UserInfoApiController {

    @Autowired
    private UserInfoService userInfoService;

    /**
     * 获取专辑购买过的声音列表集合(id)
     *
     * @param albumId
     * @return
     */
    @SakiLogin
    @Operation(summary = "获取专辑购买过的声音列表集合(id)")
    @GetMapping("/findUserPaidTrackList/{albumId}")
    public Result<List<Long>> findUserPaidTrackList(@PathVariable("albumId") Long albumId) {
        List<Long> list = userInfoService.findUserPaidTrackList(albumId, AuthContextHolder.getUserId());
        return Result.ok(list);
    }

    /**
     * 判断用户是否购买过专辑
     *
     * @param albumId
     * @return
     */
    @SakiLogin
    @Operation(summary = "判断用户是否购买过专辑")
    @GetMapping("/isPaidAlbum/{albumId}")
    public Result<Boolean> isPaidAlbum(@PathVariable("albumId") Long albumId) {
        Long userId = AuthContextHolder.getUserId();
        Boolean result = userInfoService.isPaidAlbum(albumId, userId);
        return Result.ok(result);
    }

    /**
     * 判断用户是否购买声音列表
     *
     * @param albumId
     * @param trackIdList
     * @return
     */
    @SakiLogin
    @Operation(summary = "判断用户是否购买声音列表")
    @PostMapping("/userIsPaidTrack/{albumId}")
    public Result<Map<Long, Integer>> userIsPaidTrack(@PathVariable("albumId") Long albumId, @RequestBody List<Long> trackIdList) {
        Long userId = AuthContextHolder.getUserId();
        Map<Long, Integer> map = userInfoService.userIsPaidTrack(userId, albumId, trackIdList);
        return Result.ok(map);
    }

    /**
     * 根据用户 id查询用户信息
     *
     * @param userId
     * @return
     */
    @Operation(summary = "根据用户id查询用户信息")
    @GetMapping("/getUserInfoVo/{userId}")
    public Result<UserInfoVo> getUserInfo(@PathVariable Long userId) {
        // 调用服务层方法
        UserInfoVo userInfoVo = userInfoService.getUserInfo(userId);
        // 返回数据
        return Result.ok(userInfoVo);
    }

    /**
     * 保存用户购买记录
     *
     * @param userPaidRecordVo
     * @return
     */
    @Operation(summary = "处理用户购买记录")
    @PostMapping("/savePaidRecord")
    public Result savePaidRecord(@RequestBody UserPaidRecordVo userPaidRecordVo) {
        try {
            userInfoService.savePaidRecord(userPaidRecordVo);
        } catch (Exception e) {
            return Result.fail(); // 保存失败不能返回 200 ==> Result.fail();
        }
        return Result.ok();
    }
}

